using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace Easislides
{
	public class FrmUpdateFileName : Form
	{
		private IContainer components = null;
		private TextBox tbFileName;
		private Label Mess;
		private Button BtnCancel;
		private Button BtnOK;
		private string ListText;
		private string InputDir;
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
            this.tbFileName = new System.Windows.Forms.TextBox();
            this.Mess = new System.Windows.Forms.Label();
            this.BtnCancel = new System.Windows.Forms.Button();
            this.BtnOK = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // tbFileName
            // 
            this.tbFileName.Location = new System.Drawing.Point(12, 35);
            this.tbFileName.Name = "tbFileName";
            this.tbFileName.Size = new System.Drawing.Size(309, 20);
            this.tbFileName.TabIndex = 0;
            // 
            // Mess
            // 
            this.Mess.Location = new System.Drawing.Point(12, 9);
            this.Mess.Name = "Mess";
            this.Mess.Size = new System.Drawing.Size(316, 14);
            this.Mess.TabIndex = 1;
            this.Mess.Text = "T";
            // 
            // BtnCancel
            // 
            this.BtnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.BtnCancel.Location = new System.Drawing.Point(177, 71);
            this.BtnCancel.Name = "BtnCancel";
            this.BtnCancel.Size = new System.Drawing.Size(80, 24);
            this.BtnCancel.TabIndex = 2;
            this.BtnCancel.Text = "Cancel";
            this.BtnCancel.Click += new System.EventHandler(this.BtnCancel_Click);
            // 
            // BtnOK
            // 
            this.BtnOK.Location = new System.Drawing.Point(81, 71);
            this.BtnOK.Name = "BtnOK";
            this.BtnOK.Size = new System.Drawing.Size(80, 24);
            this.BtnOK.TabIndex = 1;
            this.BtnOK.Text = "OK";
            this.BtnOK.Click += new System.EventHandler(this.BtnOK_Click);
            // 
            // FrmUpdateFileName
            // 
            this.AcceptButton = this.BtnOK;
            this.CancelButton = this.BtnCancel;
            this.ClientSize = new System.Drawing.Size(333, 113);
            this.Controls.Add(this.BtnCancel);
            this.Controls.Add(this.BtnOK);
            this.Controls.Add(this.Mess);
            this.Controls.Add(this.tbFileName);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "FrmUpdateFileName";
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Load += new System.EventHandler(this.frmUpdateFileName_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		public FrmUpdateFileName()
		{
			this.InitializeComponent();
		}
		private void frmUpdateFileName_Load(object sender, EventArgs e)
		{
			if (gf.EasiSlidesMode == gf.UsageMode.Worship)
			{
				this.InputDir = gf.WorshipDir;
				this.ListText = "Worship List";
			}
			else
			{
				this.InputDir = gf.PraiseBookDir;
				this.ListText = "PraiseBook List";
			}
			if (gf.NameChangeAction == 1)
			{
				this.Text = "Add New " + this.ListText;
				this.Mess.Text = "Please enter title of the new " + this.ListText;
			}
			else
			{
				if (gf.NameChangeAction == 2)
				{
					this.Text = "Rename " + this.ListText;
					this.Mess.Text = "Rename '" + gf.SelectedListName + "' to:";
					this.tbFileName.Text = gf.SelectedListName;
					this.tbFileName.SelectAll();
				}
				else
				{
					if (gf.NameChangeAction == 3)
					{
						this.Text = "Rename Folder '" + gf.FolderRenameName[gf.FolderRenameNo] + "'";
						this.Mess.Text = "Rename '" + gf.SelectedListName + "' to:";
						this.tbFileName.Text = gf.FolderRenameName[gf.FolderRenameNo];
						this.tbFileName.SelectAll();
					}
					else
					{
						if (gf.NameChangeAction == 6)
						{
							this.Text = "Save InfoScreen as...";
							this.Mess.Text = "Save as:";
							this.tbFileName.Text = gf.FolderRenameName[gf.FolderRenameNo];
							this.tbFileName.SelectAll();
						}
						else
						{
							if (gf.NameChangeAction == 7)
							{
								this.Text = "Copy Session to PraiseBook";
								this.tbFileName.Text = gf.GetDisplayNameOnly(ref gf.SelectedListName, false, false);
								this.tbFileName.SelectAll();
								this.InputDir = gf.PraiseBookDir;
								this.Mess.Text = "Copy '" + this.tbFileName.Text + "' as:";
							}
							else
							{
								if (gf.NameChangeAction == 8)
								{
									this.Text = "Copy PraiseBook to Session";
									this.tbFileName.Text = gf.GetDisplayNameOnly(ref gf.SelectedListName, false, false);
									this.tbFileName.SelectAll();
									this.InputDir = gf.WorshipDir;
									this.Mess.Text = "Copy '" + this.tbFileName.Text + "' as:";
								}
							}
						}
					}
				}
			}
		}
		private bool PerformAction()
		{
			string text = this.tbFileName.Text;
			bool result;
			if (gf.NameChangeAction == 1)
			{
				string str = (this.InputDir == gf.WorshipDir) ? ".esw" : ".esp";
				if (!gf.ValidateDirNameFormat(text, this.ListText + " Title"))
				{
					result = false;
					return result;
				}
				if (File.Exists(this.InputDir + text + str))
				{
					MessageBox.Show("There is already a list with the same name, please try a different name.", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
					result = false;
					return result;
				}
				gf.ValidateDir(this.InputDir, true);
				try
				{
					string inNotes = "";
					gf.SaveIndexFile(this.InputDir + text + str, ref gf.ListViewNotations, gf.UsageMode.Worship, false, gf.Def_FormatString, inNotes);
					gf.SelectedListName = text;
				}
				catch
				{
					MessageBox.Show("Error encountered whilst create new file - Please ensure you have write access to " + this.InputDir, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				}
			}
			else
			{
				if (gf.NameChangeAction == 2)
				{
					string str = (this.InputDir == gf.WorshipDir) ? ".esw" : ".esp";
					if (!gf.ValidateDirNameFormat(text, this.ListText + " Title"))
					{
						result = false;
						return result;
					}
					if (File.Exists(this.InputDir + text + str))
					{
						MessageBox.Show("There is already a list with the same name, please try a different name.", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
						result = false;
						return result;
					}
					gf.ValidateDir(this.InputDir, true);
					try
					{
						File.Move(this.InputDir + gf.SelectedListName + str, this.InputDir + text + str);
						gf.SelectedListName = text;
					}
					catch
					{
						MessageBox.Show("Error encountered whilst trying to rename the file - Please ensure you have write access to it.", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
					}
				}
				else
				{
					if (gf.NameChangeAction == 3)
					{
						string text2 = gf.FolderRenameName[gf.FolderRenameNo];
						bool flag = false;
						if (!gf.ValidateDirNameFormat(text, "New folder name"))
						{
							result = false;
							return result;
						}
						for (int i = 1; i <= 41; i++)
						{
							if (gf.FolderRenameName[i].ToLower() == text.ToLower())
							{
								flag = true;
							}
						}
						if (!flag)
						{
							gf.FolderRenameName[gf.FolderRenameNo] = text;
							result = true;
							return result;
						}
						result = false;
						return result;
					}
					else
					{
						if (gf.NameChangeAction == 7)
						{
							string text3 = gf.PraiseBookDir + gf.Trim(this.tbFileName.Text) + ".esp";
							try
							{
								if (!File.Exists(text3))
								{
									File.Copy(gf.SelectedListName, text3, true);
									result = true;
									return result;
								}
								if (MessageBox.Show("PraiseBook already exists. Overwrite it?", "Overwrite", MessageBoxButtons.YesNo) == DialogResult.Yes)
								{
									File.Copy(gf.SelectedListName, text3, true);
									result = true;
									return result;
								}
								result = false;
								return result;
							}
							catch
							{
								MessageBox.Show("Error Saving File, please make sure you have write access and try again");
								result = false;
								return result;
							}
						}
						if (gf.NameChangeAction == 8)
						{
							string text3 = gf.WorshipDir + gf.Trim(this.tbFileName.Text) + ".esw";
							try
							{
								if (!File.Exists(text3))
								{
									File.Copy(gf.SelectedListName, text3, true);
									result = true;
									return result;
								}
								if (MessageBox.Show("WorshipList already exists. Overwrite it?", "Overwrite", MessageBoxButtons.YesNo) == DialogResult.Yes)
								{
									File.Copy(gf.SelectedListName, text3, true);
									result = true;
									return result;
								}
								result = false;
								return result;
							}
							catch
							{
								MessageBox.Show("Error Saving File, please make sure you have write access and try again");
								result = false;
								return result;
							}
						}
					}
				}
			}
			result = true;
			return result;
		}
		private void BtnOK_Click(object sender, EventArgs e)
		{
			if (this.PerformAction())
			{
				base.DialogResult = DialogResult.OK;
				base.Close();
			}
		}

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }
	}
}
